package com.diandian.backend.dingding.exchange.controller;

import com.diandian.backend.dingding.exchange.service.DingdingTokenService;
import com.diandian.dingding.boot.starter.service.DingdingApplicationAddressBookDeptManageService;
import com.dingtalk.api.response.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/dept")
public class DingdingApplicationAddressBookDeptController {

    private static final Logger logger = LoggerFactory.getLogger(DingdingApplicationAddressBookDeptController.class);

    @Autowired
    private DingdingApplicationAddressBookDeptManageService dingdingApplicationAddressBookService;

    @Autowired
    private DingdingTokenService dingdingTokenService;

    /**
     * 获取部门详情
     * @param dingDeptId
     * @return
     */
    @GetMapping("/getByDeptId")
    public OapiV2DepartmentGetResponse getByDeptId(@RequestHeader(value = "appKey") String appKey,
                                                   @RequestHeader(value = "appSecret") String appSecret,
                                                   @RequestParam(value = "dingDeptId") Long dingDeptId) throws Exception {
        String token = dingdingTokenService.getToken(appKey, appSecret);
        return dingdingApplicationAddressBookService.getDeptInfoByDeptId(token, dingDeptId);
    }

    /**
     * 获取部门列表
     * @param dingDeptId
     * @return
     */
    @GetMapping("/getDeptList")
    public OapiV2DepartmentListsubResponse getDeptList(@RequestHeader(value = "appKey") String appKey,
                                                       @RequestHeader(value = "appSecret") String appSecret,
                                                       @RequestParam(value = "dingDeptId") Long dingDeptId,
                                                       @RequestParam(value = "language") String language) throws Exception {
        String token = dingdingTokenService.getToken(appKey, appSecret);
        return dingdingApplicationAddressBookService.getDeptList(token, dingDeptId, language);
    }

    /**
     * 获取子部门ID列表
     * @param dingDeptId
     * @return
     */
    @GetMapping("/getDeptIdList")
    public OapiV2DepartmentListsubidResponse getDeptIdList(@RequestHeader(value = "appKey") String appKey,
                                                           @RequestHeader(value = "appSecret") String appSecret,
                                                           @RequestParam(value = "dingDeptId") Long dingDeptId) throws Exception {
        String token = dingdingTokenService.getToken(appKey, appSecret);
        return dingdingApplicationAddressBookService.getDeptIdList(token, dingDeptId);
    }

    /**
     * 获取指定部门的所有父部门列表
     * @param dingDeptId
     * @return
     */
    @GetMapping("/getParentDeptList")
    public OapiV2DepartmentListparentbydeptResponse getParentDeptList(@RequestHeader(value = "appKey") String appKey,
                                                                      @RequestHeader(value = "appSecret") String appSecret,
                                                                      @RequestParam(value = "dingDeptId") Long dingDeptId) throws Exception {
        String token = dingdingTokenService.getToken(appKey, appSecret);
        return dingdingApplicationAddressBookService.getParentDeptList(token, dingDeptId);
    }

    /**
     * 获取指定用户的所有父部门列表
     * @return
     */
    @GetMapping("/getParentDeptListByUserId")
    public OapiV2DepartmentListparentbyuserResponse getParentDeptListByUserId(@RequestHeader(value = "appKey") String appKey,
                                                                              @RequestHeader(value = "appSecret") String appSecret,
                                                                              @RequestParam(value = "userId") String userId) throws Exception {
        String token = dingdingTokenService.getToken(appKey, appSecret);
        return dingdingApplicationAddressBookService.getParentDeptListByUserId(token, userId);
    }
}
